package com.ddlaw.serviceImpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddlaw.entity.TUsers;
import com.ddlaw.mapper.TUsersMapper;
import com.ddlaw.service.UserService;
import com.ddlaw.util.MD5Util;
import com.ddlaw.util.SerialNumberUtil;
@Service
@Transactional
public class UserServiceImpl implements UserService{
	@Resource
	TUsersMapper usersMapper;
	
	@Override
	public TUsers getUserById(int userId) {
		return usersMapper.getUserById(userId);
	}
	
	@Override
	public void addQuestion() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void addUser(TUsers users) {
		String code = SerialNumberUtil.toSerialNumber(20);
		String password = MD5Util.MD5(MD5Util.MD5(users.getPassword())+code);
		users.setRandomcode(code);
		users.setPassword(password);
		usersMapper.addUser(users);
	}
	
	@Override
	public TUsers getUserByName(String username) {
		// TODO Auto-generated method stub
		return usersMapper.getUserByName(username);
	}

	@Override
	public TUsers login(String username, String password) {
		// TODO Auto-generated method stub
		TUsers user = usersMapper.getUserByName(username);
		if(user != null && MD5Util.MD5(MD5Util.MD5(password)+user.getRandomcode()).equals(user.getPassword())){
			return user;
		}
			return null;
	}
}
